package hbase;

import com.ruoyi.hadoop.hbase.HBaseUtils;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class HbaseV2 {

    private HBaseAdmin admin;

    @Before
    public void before() {
        admin = (HBaseAdmin) HBaseUtils.getAdmin();
    }

    @Test
    public void createTable() throws IOException {
        //1.创建表的表述器对象
        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("user_info"));
        //2. 创建列簇的描述器
        HColumnDescriptor familyColumn1 = new HColumnDescriptor("base_info");
        HColumnDescriptor familyColumn2 = new HColumnDescriptor("extra_info");
        //3.添加列簇
        tableDescriptor.addFamily(familyColumn1);
        tableDescriptor.addFamily(familyColumn2);
        //4.提交表的描述器从而建表
        admin.createTable(tableDescriptor);
    }

    @Test
    public void listTableColumnFamilies() throws IOException {
        //1. 获取到表的描述器
        HTableDescriptor user_info = admin.getTableDescriptor(TableName.valueOf("user_info"));
        //2. 获取到列簇信息
        HColumnDescriptor[] columnFamilies = user_info.getColumnFamilies();
        //3. 遍历
        for(HColumnDescriptor columnFamily : columnFamilies) {
            System.out.println(columnFamily.getNameAsString());
        }
    }

    @Test
    public void alterTable1() throws IOException {
        //1. 获取到表的描述器
        TableName tableName = TableName.valueOf("user_info");
        HTableDescriptor user_info = new HTableDescriptor(tableName);
        //2. 获取到列簇信息
        HColumnDescriptor familyColumn1 = new HColumnDescriptor("lixi_info");
        user_info.addFamily(familyColumn1);
        //3. 提交修改
        admin.modifyTable(tableName, user_info);
    }

    @Test
    public void alterTable2() throws IOException {
        //1. 获取到表的描述器
        TableName tableName = TableName.valueOf("user_info");
        HTableDescriptor user_info = admin.getTableDescriptor(tableName);
        //2. 获取到列簇信息
        HColumnDescriptor familyColumn1 = new HColumnDescriptor("lixi_info");
        HColumnDescriptor familyColumn2 = new HColumnDescriptor("rock_info");
        user_info.addFamily(familyColumn1);
        user_info.addFamily(familyColumn2);
        //3. 提交修改
        admin.modifyTable(tableName, user_info);
    }

    @Test
    public void deleteColumnFamily1() throws IOException {
        //1. 获取到表的描述器
        TableName tableName = TableName.valueOf("user_info");
        HTableDescriptor user_info = admin.getTableDescriptor(tableName);
        //2. 删除列簇
        user_info.removeFamily(Bytes.toBytes("lixi_info"));
        //3. 提交修改
        admin.modifyTable(tableName, user_info);
    }

    @Test
    public void deleteColumnFamily2() throws IOException {
        //1. 获取到表的描述器
        TableName tableName = TableName.valueOf("user_info");
        //2. 提交删除
        admin.deleteColumn(tableName, Bytes.toBytes("rock_info"));
    }

    /**
     *
     * 修改列簇的属性信息
     */
    @Test
    public void modifyColumnFamily() throws IOException {
        //1. 获取到表的描述器
        TableName tableName = TableName.valueOf("user_info");
        //2. 列簇
        HColumnDescriptor familyColumn1 = new HColumnDescriptor("base_info");
        //2. 提交删除
        admin.modifyColumn(tableName, familyColumn1);
    }

    @Test
    public void deleteTable() throws IOException {
        //1. 获取到表的描述器
        TableName tableName = TableName.valueOf("user_info");
        //2. 处理表是否失效
        if(!admin.isTableDisabled(tableName)) {
            admin.disableTable(tableName);
        }
        //3. 删除表
        admin.deleteTable(tableName);
    }

    @After
    public void after() {
        HBaseUtils.close(admin);
    }
}
